﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Configuration;
using System.Xml;

using ECCIT;
using OgilvyOne.UKNow.Model;
using OgilvyOne.UKNow.BLL;
using OgilvyOne.UKNow.Utility;

using System.Net;
using System.IO;

namespace OgilvyOne.UKNow.Web
{
    public partial class XmlHttpGate : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Response.ContentType = "text/xml";
            Response.ContentEncoding = Encoding.UTF8;

            try
            {
                string cmd = Request.Params["cmd"];
                string sLangID = Request.Params["langID"];
                int langID = (sLangID == null || sLangID.Trim() == string.Empty) ? 1033 : int.Parse(sLangID);
                string output = null;

                switch (cmd)
                {
                    case "1":
                        int wid = int.Parse(Request.Params["wid"]);
                        WarmupBLL bll = new WarmupBLL(langID);
                        WarmupModel model = bll.GetWarmupById(wid);
                        output = JsonUtil.Serialize(model);
                        break;
                    case "2":
                        
                        int warmid=int.Parse(Request.Params["wid"]);
                        int pagingIndex=int.Parse(Request.Params["pageIndex"]);
                        PagingParameter pp = new PagingParameter(true, pagingIndex, 5, null);
                        WarmupsCommentBLL warmBll = new WarmupsCommentBLL(langID);
                        PagingList list = warmBll.GetCommentIndexPage(warmid,pp);
                        output = JsonUtil.Serialize(list);
                        break;
                    case "3":    // 发现英国元素发布作品页删除上传图片, 刘扬添加
                        string imgName = Request.Params["imgName"];
                        bll = new WarmupBLL(langID);
                        ResultInfo result = bll.DeleteUploadedImage(imgName);
                        output = JsonUtil.Serialize(result);
                        break;
                    case "4":   // 保存发现英国元素作品数据，刘扬添加
                        bll = new WarmupBLL(langID);
                        XmlDocument doc = new XmlDocument();
                        doc.Load(Request.InputStream);
                        imgName = doc.GetElementsByTagName("imgName")[0].InnerText;
                        string desc = doc.GetElementsByTagName("desc")[0].InnerText;
                        AccountController ac = new AccountController(langID);
                        if (ac.AccountInfo == null)
                        {
                            result = new ResultInfo(false);
                            result.Message = "当前操作用户未登录";
                        }
                        else
                        {
                            WarmupModel wm = new WarmupModel(langID, null, imgName, desc, ac.AccountInfo.ID, 1, 0, 0, 0, DateTime.Now, DateTime.Now);
                            result = bll.Save(wm);
                        }
                        output = JsonUtil.Serialize(result);
                        break;
                    case "5":   // 获取发现英国元素作品数据，刘扬添加
                        int currPageIndex = int.Parse(Request.Params["currPageIndex"]);
                        string sortType = Request.Params["sortType"];
                        string sortDirect = Request.Params["sortDirect"];
                        bll = new WarmupBLL(langID);
                        int pageSize = 12;
                        PagingList models = bll.GetGetPublishedWarmupModels(currPageIndex, pageSize, sortType, sortDirect);
                        output = JsonUtil.Serialize(models);
                        break;
                    case "999": // 获取用户session，刘扬添加
                        AccountController act = new AccountController(langID);
                        output = JsonUtil.Serialize(act.AccountInfo);
                        break;

                    case "doVote": // 为英国元素中的作品投票
                        int warmId = int.Parse(Request.Params["wid"]);
                        bll = new WarmupBLL(langID);
                        ResultInfo res= bll.SetVote(warmId);
                        /*
                         * 代码修改
                         * 修改人员：刘扬
                         * 修改日期：2012-02-09
                         * 修改原因：如果投票成功，返回作品当前投票数
                         * */
                        if (res.Result)
                            res.ReturnObject = bll.GetWarmupById(warmId).VoteCount;

                        output = JsonUtil.Serialize(res);
                        break;
                    case "sendComm":   // 保存发现英国元素作品评论数据，cp添加
                        warmId = int.Parse(Request.Params["wid"]);
                        WarmupsCommentBLL wbll = new WarmupsCommentBLL(langID);
                        XmlDocument docu = new XmlDocument();
                        docu.Load(Request.InputStream);
                        string descr = docu.GetElementsByTagName("desc")[0].InnerText;
                        AccountController acc = new AccountController(langID);
                        if (acc.AccountInfo == null)
                        {
                            result = new ResultInfo(false);
                            result.Message = "当前操作用户未登录";
                        }
                        else
                        {
                            WarmupCommentModel wm = new WarmupCommentModel(langID,null, warmId, descr,DateTime.Now,1, acc.AccountInfo.ID,null);
                            ResultInfo resss=wbll.Save(wm);
                            int? warid=((WarmupCommentModel)resss.ReturnObject).ID;
                           
                            //调用关键字接口
                            LogMapKeyWordsBLL MkwBll = new LogMapKeyWordsBLL(langID);
                            Dictionary<MapKeyWordsModel, int> dmw = MkwBll.seleRepetKeyWordsModelList(descr);
                            new LogMapKeyWordsBLL(langID).InsertLog(dmw, Convert.ToInt32(warid), 1);

                            result =resss; 
                        }
                        output = JsonUtil.Serialize(result);
                        break;
                    case "getArtForm":  // 获取艺术形式数据，刘扬添加
                        ArtFormBLL afb = new ArtFormBLL();
                        IList<ArtFormModel> arms = afb.selectArt(langID);
                        output = JsonUtil.Serialize(arms);
                        break;
                    case "events_comm": //获取活动评论 cp

                        int eid = int.Parse(Request.Params["eid"]);
                        int indexpage = int.Parse(Request.Params["pageIndex"]);
                        PagingParameter pagep = new PagingParameter(true, indexpage, 5, null);
                        EventsCommentBLL eventsBll = new EventsCommentBLL(langID);
                        PagingList plist = eventsBll.getEventsCommentPage(eid, pagep);
                        output = JsonUtil.Serialize(plist);
                        break;


                    case "login":   
                        string email = Request.Params["email"];
                        string pwd = Request.Params["pwd"];

                        AccountController account = new AccountController(langID);
                        ResultInfo resu =account.ProcessLogin(email, pwd);

                        output = JsonUtil.Serialize(resu);
                        break;

                    case "GetAllPublishedEventsYearMonth":
                        EventsBLL eBll = new EventsBLL(langID);
                        output = JsonUtil.Serialize(eBll.GetAllPublishedEventsYearMonth());
                        break;
                    case "GetAllPublishedEventsCities":
                        eBll = new EventsBLL(langID);
                        string month = Request.Params["month"];
                        output = JsonUtil.Serialize(eBll.GetEventsCitiesByYearMonth(month));
                        break;

                    case "Login_state":  //验证是否登录
                        MemberModel Memmodel=new AccountController(langID).AccountInfo;
                        ResultInfo resul = new ResultInfo();
                        if (Memmodel == null)
                        {
                            resul.Result = false;
                            resul.Message = "当前操作用户未登录";
                        }
                        else {
                            resul.Result = true;
                            resul.ReturnObject = Memmodel;

                        }
                        output = JsonUtil.Serialize(resul);
                        break;

                    case "Logout":  //退出登录
                       bool flag= new AccountController(langID).ProcessLogout();

                       output = JsonUtil.Serialize(flag);
                        break;
                    case "GetFilteredEventsModels":
                        eBll = new EventsBLL(langID);
                        docu = new XmlDocument();
                        docu.Load(Request.InputStream);
                        string artForms = docu.GetElementsByTagName("artForms")[0].InnerText;
                        string yearMonth = docu.GetElementsByTagName("yearMonth")[0].InnerText;
                        string dates = docu.GetElementsByTagName("dates")[0].InnerText;
                        string cities = docu.GetElementsByTagName("cities")[0].InnerText;
                        currPageIndex = int.Parse(Request.Params["currPageIndex"]);
                        output = JsonUtil.Serialize(eBll.GetFilteredEventsModels(artForms, yearMonth, dates, cities, currPageIndex));
                        break;
                    case "GetEventsStartDateInPeriod":
                        eBll = new EventsBLL(langID);
                        string sd = Request.Params["startDate"];
                        string ed = Request.Params["endDate"];
                        output = JsonUtil.Serialize(eBll.GetEventsStartDateInPeriod(sd, ed));
                        break;
                    case "GetCityName":
                        string sCityID = Request.Params["cityID"];
                        if (!string.IsNullOrEmpty(sCityID))
                            output = JsonUtil.Serialize(DictionaryUtil.GetCityByID(int.Parse(sCityID)));
                        else
                            output = string.Empty;
                        break;
                    case "GetCurrentYearMonth":
                        string y = DateTime.Now.Year.ToString();
                        int m = DateTime.Now.Month;
                        string sm = m.ToString();
                        if (m < 10) sm = "0" + sm;
                        output = JsonUtil.Serialize(y + "-" + sm);
                        break;
                    case "GetEventsCitiesByYearMonth":
                        eBll = new EventsBLL(langID);
                        string ym = Request.Params["yearmonth"];
                        output = JsonUtil.Serialize(eBll.GetEventsCitiesByYearMonth(ym));
                        break;

                    case "sendRespond":   // 
                        warmId = int.Parse(Request.Params["wid"]);
                        int cid = int.Parse(Request.Params["cid"]);
                        WarmupsCommentBLL rwbll = new WarmupsCommentBLL(langID);
                       
                        XmlDocument rdocu = new XmlDocument();
                        rdocu.Load(Request.InputStream);
                        string rdescr = rdocu.GetElementsByTagName("desc")[0].InnerText;
                        AccountController racc = new AccountController(langID);
                        if (racc.AccountInfo == null)
                        {
                            result = new ResultInfo(false);
                            result.Message = "当前操作用户未登录";
                        }
                        else
                        {
                            WarmupCommentModel wm = new WarmupCommentModel();
                            wm.LanguageID = langID;
                            wm.WarmupID = warmId;
                            wm.Text = rdescr;
                            wm.Time = DateTime.Now;
                            wm.State = 1;
                            wm.CommenterID = racc.AccountInfo.ID;
                            wm.SourceCommenterID = cid;
                            result = rwbll.Save(wm);
                            
                        }
                        output = JsonUtil.Serialize(result);
                        break;
                    case "GetUploadFilesByEventsID":
                        // 默认pageSize为8
                        pageSize = 8;
                        string sEid = Request.Params["eid"];    // 获取活动id
                        eid = int.Parse(sEid);
                        eBll = new EventsBLL(langID);
                        currPageIndex = int.Parse(Request.Params["currPageIndex"]);
                        PagingParameter pagingParams = PagingParameterFactory.CreateInstance(true, currPageIndex, pageSize, null);
                        output = JsonUtil.Serialize(eBll.GetUploadFilesByEventsID(eid, pagingParams));
                        break;
                    case "sendEventComm":   // 保存活动评论数据，cp添加
                        int earmId = int.Parse(Request.Params["wid"]);
                        EventsCommentBLL ebll = new EventsCommentBLL(langID);
                        XmlDocument edocu = new XmlDocument();
                        edocu.Load(Request.InputStream);
                        string edescr = edocu.GetElementsByTagName("desc")[0].InnerText;
                        AccountController eacc = new AccountController(langID);
                        if (eacc.AccountInfo == null)
                        {
                            result = new ResultInfo(false);
                            result.Message = "当前操作用户未登录";
                        }
                        else
                        {
                            EventsCommentModel wm = new EventsCommentModel();

                            wm.LanguageID = langID;
                            wm.EventsID = earmId;
                            wm.Content = edescr;
                            wm.Time = DateTime.Now;
                            wm.MemberID = eacc.AccountInfo.ID;

                            result = ebll.Save(wm);
                            //调用关键字日志接口
                            int? evid = ((EventsCommentModel)result.ReturnObject).ID;
                            //MapKeyWordsBLL MkwBll = new MapKeyWordsBLL(langID);
                            //Dictionary<MapKeyWordsModel, int> dmw = MkwBll.seleRepetKeyWordsModelList(edescr);
                            //new LogMapKeyWordsBLL(langID).InsertLog(dmw, Convert.ToInt32(evid), 0);
                            LogMapKeyWordsBLL MkwBll = new LogMapKeyWordsBLL(langID);
                            Dictionary<MapKeyWordsModel, int> dmw = MkwBll.seleRepetKeyWordsModelList(edescr);
                            new LogMapKeyWordsBLL(langID).InsertLog(dmw, Convert.ToInt32(evid), 0);
                        }
                        output = JsonUtil.Serialize(result);
                        break;


                    case "sendEventRespond": ;  // 
                       int  eventsid= int.Parse(Request.Params["wid"]);
                        int eventcid = int.Parse(Request.Params["cid"]);
                        EventsCommentBLL eventbll = new EventsCommentBLL(langID);
                       
                        XmlDocument xdocu = new XmlDocument();
                        xdocu.Load(Request.InputStream);
                        string xdescr = xdocu.GetElementsByTagName("desc")[0].InnerText;
                        AccountController xacc = new AccountController(langID);
                        if (xacc.AccountInfo == null)
                        {
                            result = new ResultInfo(false);
                            result.Message = "当前操作用户未登录";
                        }
                        else
                        {
                            EventsCommentModel wm = new EventsCommentModel();
                            wm.LanguageID = langID;
                            wm.EventsID = eventsid;
                            wm.Content = xdescr;
                            wm.Time = DateTime.Now;
                            
                            wm.MemberID = xacc.AccountInfo.ID;
                            wm.SourceMemberID = eventcid;
                            result = eventbll.Save(wm);
                            
                        }
                        output = JsonUtil.Serialize(result);
                        break;
                }

                Response.Write(output);
            }
            catch (Exception ex)
            {
                Response.Write("<Error><![CDATA[Error occurred in XmlHttpGate. Phase: 1. Message: " + ex.Message + "]]></Error>");
            }
        }

       

    }
}